package cn.stu.framework.service.impl;

import cn.hutool.core.text.CharSequenceUtil;
import cn.stu.common.utils.ApiData;
import cn.stu.framework.dto.MenuDto;
import cn.stu.framework.entity.Menu;
import cn.stu.framework.mapper.MenuMapper;
import cn.stu.framework.service.MenuService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;


/**
 * 菜单服务实施
 *
 * @author 郑志豪
 * @date 2023/09/11
 */
@Service
public class MenuServiceImpl extends ServiceImpl<MenuMapper, Menu> implements MenuService {


    /**
     * 菜单列表
     *
     * @param dto DTO
     * @return {@link ApiData}
     */
    @Override
    public ApiData menuList(MenuDto dto) {
        List<Menu> menus = lambdaQuery().like(CharSequenceUtil.isNotBlank(dto.getTitle()), Menu::getTitle, dto.getTitle())
                .eq(dto.getPid() != null, Menu::getPid, dto.getPid())
                .or()
                .eq(dto.getPid() != null, Menu::getMenuId, dto.getPid()).list();
        return ApiData.getTrue("获取成功", menus);
    }

    /**
     * 菜单添加
     *
     * @param add 添加
     * @return {@link ApiData}
     */
    @Override
    public ApiData menuAdd(Menu add) {
        if (CharSequenceUtil.isBlank(add.getTitle())) {
            return ApiData.getFalse("菜单名为空！");
        }
        if (CharSequenceUtil.isBlank(add.getPath())) {
            return ApiData.getFalse("路由地址为空！");
        }
        if (CharSequenceUtil.isBlank(add.getComponent())) {
            return ApiData.getFalse("组件路径为空！");
        }
        if (add.getPid() != 0) {
            boolean exists = baseMapper.exists(lambdaQuery().eq(Menu::getMenuId, add.getPid()).getWrapper());
            if (!exists) {
                return ApiData.getFalse("父菜单不存在！");
            }
        }
        if (CharSequenceUtil.isBlank(add.getPath())) {
            return ApiData.getFalse("菜单地址不存在!");
        }
        baseMapper.insert(add);
        return ApiData.getTrue("添加成功！");
    }

    /**
     * 菜单更新
     *
     * @param update 更新
     * @return {@link ApiData}
     */
    @Override
    public ApiData menuUpdate(Menu update) {
        if (update.getMenuId() == null) {
            return ApiData.getFalse("菜单id不能为空！");
        }
        if (Objects.equals(update.getMenuId(), update.getPid())) {
            return ApiData.getFalse("不能成为自己的子菜单！");
        }
        if (CharSequenceUtil.isBlank(update.getPath())) {
            return ApiData.getFalse("路由地址为空！");
        }
        if (CharSequenceUtil.isBlank(update.getComponent())) {
            return ApiData.getFalse("组件路径为空！");
        }
        if (CharSequenceUtil.isBlank(update.getTitle())) {
            return ApiData.getFalse("菜单名为空！");
        }
        if (update.getPid() != 0) {
            boolean exists = baseMapper.exists(lambdaQuery().eq(Menu::getMenuId, update.getPid()).getWrapper());
            if (!exists) {
                return ApiData.getFalse("父菜单不存在！");
            }
        }
        if (update.getPid() != 1 && (CharSequenceUtil.isBlank(update.getPath()))) {
            return ApiData.getFalse("菜单地址不存在!");
        }
        baseMapper.updateById(update);
        return ApiData.getTrue("修改成功！");
    }

    /**
     * 删除菜单
     *
     * @param menuId 菜单ID
     * @return {@link ApiData}
     */
    @Override
    public ApiData menuDel(Long menuId) {
        boolean exists = baseMapper.exists(lambdaQuery().eq(Menu::getMenuId, menuId).getWrapper());
        if (!exists) {
            return ApiData.getFalse("参数错误！");
        }
        removeById(menuId);
        return ApiData.getTrue("删除成功！");
    }

    /**
     * 菜单信息
     *
     * @param menuId 菜单ID
     * @return {@link ApiData}
     */
    @Override
    public ApiData menuInfo(Long menuId) {
        if (menuId == null) {
            return ApiData.getTrue("获取成功", list());
        }
        Menu m = baseMapper.selectById(menuId);
        if (m == null) {
            return ApiData.getFalse("id不存在！");
        }
        return ApiData.getTrue("获取成功", m);
    }

    @Override
    public List<Menu> getMenuListByMenuIds(List<Long> mIds) {
        return baseMapper.selectList(lambdaQuery().in(Menu::getMenuId, mIds).getWrapper());
    }
}
